-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJump search.cpp
47 lines (41 loc) · 1.16 KB
/
Jump search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cmath>
using namespace std;
void jumpSearch(int A[], int n, int key){
int prev = 0, jump = sqrt(n);
while(key > A[jump-1] && jump < n){
prev = jump;
jump += sqrt(n); // or, jump += jump;
if (jump >= n) break;
}
// Linear search
int result = 0, Li = 0;
for(int i = prev; i < jump; i++, Li++){
if(i >= n) break;
if(A[i] == key){
result = i+1;
break;
}
}
if(result > 0){
cout << "The element [" << key << "] is matched at the position (" << result << ")" << endl;
} else {
cout << "The element [" << key << "] didn't match with any element in the array." << endl;
}
cout << "Total jump(s): " << jump/int(sqrt(n)) << endl;
cout << "Linear iteration(s): " << Li << endl;
}
void print(int A[], int n){
for (int i = 0; i < n; i++) {
cout << "[" << A[i] << "] ";
}
cout << endl;
}
int main(){
int A[] = {0, 2, 3, 5, 8, 12, 25, 33, 48, 66, 78, 123, 456}; // Must be a sorted array
int n = sizeof(A)/sizeof(A[0]);
print(A, n);
cout << endl;
int key = 33;
jumpSearch(A, n, key);
}